package proteinprediction.arffgenerator;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.List;

public abstract class AbstractArffGenerator {

	protected String structPath = "";
	protected String resultPath = "";
	protected List<String> proteins;
	protected boolean considerZeros;
	abstract public void generateArff();
	
	protected void createArffFileForIMP(char[] array, String arffAbsolutePath) throws IOException {
	    File f = new File(arffAbsolutePath);
	    if(!f.exists()){
	    	f.createNewFile();
	    }
		BufferedWriter writer = new BufferedWriter(new FileWriter(f));
		writer.write("%Created by Group77");
		writer.newLine();
		writer.write("@RELATION\t'" + "prot.arff'");
		writer.newLine();
		writer.newLine();
		writer.write("@ATTRIBUTE\tpos\tNUMERIC");
		writer.newLine();
		writer.write("@ATTRIBUTE\trelpos\tNUMERIC");
		writer.newLine();
		if (considerZeros) {
			writer.write("@ATTRIBUTE\tclass\t{+,-,0}");
		} else {
			writer.write("@ATTRIBUTE\tclass\t{+,-}");
		}
		writer.newLine();
		writer.newLine();
		writer.write("@DATA");
		DecimalFormat dFormat = new DecimalFormat("#0.00");
		for (int i = 0; i < array.length; i++) {
			writer.newLine();
			writer.write(i + "," + dFormat.format(Math.round(((double)i / (array.length - 1)) * 100) / 100.0)   + "," + evaluateChar(array[i]));
		}
		writer.close();
	}
	
	private String evaluateChar(char c) {
		String result = "";
		if (c == 'H' || c == 'L') {
			result = "+";
		} else if (c == ' ' && considerZeros) {
			result = "0";
		} else {
			result = "-";
		}
		return result;
	}
	
	protected void createArffFileForSOL(String name, String arffAbsolutePath, String solQueryInPath) throws IOException {
	    File f=new File(arffAbsolutePath);
	    if(!f.exists()){
	    	f.createNewFile();
	    }
		BufferedWriter writer = new BufferedWriter(new FileWriter(f));
		writer.write("%Created by Group77");
		writer.newLine();
		writer.write("@RELATION\t'" + "prot.arff'");
		writer.newLine();
		writer.newLine();
		writer.write("@ATTRIBUTE\tpos\tNUMERIC");
		writer.newLine();
		writer.write("@ATTRIBUTE\trelpos\tNUMERIC");
		writer.newLine();
		if (considerZeros) {
			writer.write("@ATTRIBUTE\tclass\t{+,-,0}");
		} else {
			writer.write("@ATTRIBUTE\tclass\t{+,-}");
		}
		writer.newLine();
		writer.newLine();
		writer.write("@DATA");
		int solLength = getSequenceLengthForSolProtein(solQueryInPath);
		DecimalFormat dFormat = new DecimalFormat("#0.00");
		for (int i = 0; i < solLength; i++) {
			writer.newLine();
			writer.write(i + "," + dFormat.format(Math.round(((double)i / (solLength - 1)) * 100) / 100.0) + "," + "-");
		}
		writer.close();
	}
	
	protected int getSequenceLengthForSolProtein(String solQueryInPath) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(solQueryInPath));
		reader.readLine();
		String resultString = "";
		String thisLine;
		while ((thisLine = reader.readLine())!= null) {
			resultString = resultString + thisLine;
		}
		reader.close();
		return resultString.length();
	}
}
